﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace PIM_ATE
{
    public class ClsDataConfigLocalBuilder : ClsDataConfigBase
    {
        public ClsDataConfigLocalBuilder(string alias)
            : base(alias)
        {
            CONFIG_TYPE = "ANTENNA";
            CONDITION_SQL = " and [Type] = '" + CONFIG_TYPE + "';";
        }

        #region static function

        #region serialize

        public static ClsDataConfigLocal[] DeserializeFile(string xmlFilePath)
        {
            try
            {
                ClsDataConfigLocal[] SerializerObject;
                var xmlSerializer = new XmlSerializer(typeof(ClsDataConfigLocal[]));
                using (var sr = new StreamReader(xmlFilePath))
                {
                    SerializerObject = (ClsDataConfigLocal[])xmlSerializer.Deserialize(sr);
                }

                return SerializerObject;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return null;
            }
        }

        public static ClsDataConfigLocal[] DeserializeString(string origin)
        {
            try
            {
                ClsDataConfigLocal[] SerializerObject;
                var xmlSerializer = new XmlSerializer(typeof(ClsDataConfigLocal[]));
                using (TextReader reader = new StringReader(origin))
                {
                    SerializerObject = (ClsDataConfigLocal[])xmlSerializer.Deserialize(reader);
                }

                return SerializerObject;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return null;
            }
        }

        public static string SerializeString(ClsDataConfigLocal[] serializerObject)
        {
            try
            {
                var xmlSerializer = new XmlSerializer(typeof(ClsDataConfigLocal[]));
                StringBuilder sb = new StringBuilder();
                XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings() { OmitXmlDeclaration = true });
                xmlSerializer.Serialize(writer, serializerObject);
                writer.Close();

                return XmlUtil.FormatXml(sb.ToString());
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return "";
            }
        }

        public static bool SerializeFile(string sPath, ClsDataConfigLocal[] serializerObject)
        {
            try
            {
                var xmlSerializer = new XmlSerializer(typeof(ClsDataConfigLocal[]));
                StringBuilder sb = new StringBuilder();
                StreamWriter writer = new StreamWriter(sPath);
                xmlSerializer.Serialize(writer, serializerObject);
                writer.Close();

                //XmlUtil.ToSoap(serializerObject, sPath);

                return true;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
                return false;
            }
        }

        #endregion

        #region 解析Config

        public static List<string> getConfigColNames()
        {
            List<string> names = new List<string>();
            names.Add("ID");
            names.Add("ProCode");
            names.Add("PIMItemName");
            names.Add("BandAlias");
            names.Add("RCUIndex");
            names.Add("TestAngle");
            names.Add("IsKnock");
            names.Add("TestIndex");
            names.Add("Message");
            names.Add("IsSweepTest");
            names.Add("PIMOrder");
            names.Add("F1Freq");
            names.Add("F2Freq");
            names.Add("PIMFreq");
            names.Add("TX1H");
            names.Add("TX1L");
            names.Add("TX2H");
            names.Add("TX2L");
            names.Add("RXH");
            names.Add("RXL");
            names.Add("TestStep1");
            names.Add("TestStep2");
            names.Add("TestTime");
            names.Add("F1Power");
            names.Add("F2Power");
            names.Add("SpecReg");
            names.Add("SpecUnit");

            names.Add("FileVer");
            return names;
        }

        public static DataTable toTable(List<ClsDataConfigLocal> lstConfig)
        {
            List<string> cols = getConfigColNames();
            DataTable dt = new DataTable();
            for (int i = 0; i < cols.Count; i++)
            {
                dt.Columns.Add(cols[i]);
            }

            for (int n = 0; n < lstConfig.Count; n++)
            {
                int i = 0;
                DataRow dr = dt.NewRow();
                //dr[i++] = lstConfig[n].Enable;
                dr[i++] = lstConfig[n].ID;
                dr[i++] = lstConfig[n].ProCode;

                dr[i++] = lstConfig[n].PIMItemName;
                dr[i++] = lstConfig[n].BandAlias;
                dr[i++] = lstConfig[n].RCUIndex;
                dr[i++] = lstConfig[n].TestAngle;
                dr[i++] = lstConfig[n].IsKnock;
                dr[i++] = lstConfig[n].TestIndex;
                dr[i++] = lstConfig[n].Message;

                dr[i++] = lstConfig[n].IsSweepTest;
                dr[i++] = lstConfig[n].PIMOrder;
                dr[i++] = lstConfig[n].F1Freq;
                dr[i++] = lstConfig[n].F2Freq;
                dr[i++] = lstConfig[n].PIMFreq;
                dr[i++] = lstConfig[n].TX1L;
                dr[i++] = lstConfig[n].TX1H;
                dr[i++] = lstConfig[n].TX2L;
                dr[i++] = lstConfig[n].TX2H;
                dr[i++] = lstConfig[n].RXL;
                dr[i++] = lstConfig[n].RXH;                
                dr[i++] = lstConfig[n].TestStep1;
                dr[i++] = lstConfig[n].TestStep2;
                dr[i++] = lstConfig[n].TestTime;
                dr[i++] = lstConfig[n].F1Power;
                dr[i++] = lstConfig[n].F2Power;
                dr[i++] = lstConfig[n].SpecReg;
                dr[i++] = lstConfig[n].SpecUnit;

                dr[i++] = lstConfig[n].FileVer;
                dt.Rows.Add(dr);
            }
            return dt;
        }

        public static List<string> getPIMColNames()
        {//显示头表
            List<string> names = new List<string>();
            names.Add("测试项目");
            names.Add("测试频段");
            names.Add("RCU序列");
            names.Add("角度");
            names.Add("测试要求");
            names.Add("测试最大值");
            names.Add("判定结果");
            names.Add("实时测试值");
            return names;
        }

        #endregion

        #endregion

        public bool upload(string sProcode, string sVersion, List<ClsDataConfigLocal> SerializerObject, ref string error_info)
        {//上传更新配置
            string strXml = SerializeString(SerializerObject.ToArray());
            if (strXml == "")
            {
                return false;
            }

            return uploadXml(sProcode, sVersion, strXml, ref error_info);
        }

        public List<ClsDataConfigLocal> download()
        {//下载配置
            string sXml = downloadXml();
            if (sXml != null || sXml != "")
            {
                var lstConfig = new List<ClsDataConfigLocal>();
                lstConfig.AddRange(DeserializeString(sXml));
                return lstConfig;
            }
            return null;
        }

        public List<ClsDataConfigLocal> download(out DataTable dtShow)
        {//下载服务器的配置
            dtShow = null;
            List<ClsDataConfigLocal> lstConfig = download();
            if (lstConfig == null) return null;

            dtShow = new DataTable();
            string[] colNames = getPIMColNames().ToArray();
            for (int i = 0; i < colNames.Length; i++)
            {
                dtShow.Columns.Add(colNames[i]);
            }
            for (int i = 0; i < lstConfig.Count; i++)
            {
                DataRow row = dtShow.NewRow();
                row[0] = lstConfig[i].PIMItemName;
                row[1] = lstConfig[i].BandAlias;
                row[2] = lstConfig[i].RCUIndex;
                row[3] = lstConfig[i].TestAngle;
                row[4] = lstConfig[i].SpecReg;
                dtShow.Rows.Add(row);
            }

            return lstConfig;
        }

        public List<ClsDataConfigLocal> download(string sXmlFilePath, out DataTable dtShow)
        {//打开本地配置
            dtShow = null;
            string xmlFilePath = sXmlFilePath;
            var serialObj = DeserializeFile(xmlFilePath);
            if (serialObj == null)
            {
                return null;
            }

            var lstConfig = new List<ClsDataConfigLocal>();
            lstConfig.AddRange(serialObj);

            dtShow = new DataTable();
            string[] colNames = getPIMColNames().ToArray();
            for (int i = 0; i < colNames.Length; i++)
            {
                dtShow.Columns.Add(colNames[i]);
            }

            for (int i = 0; i < lstConfig.Count; i++)
            {
                DataRow row = dtShow.NewRow();
                row[0] = lstConfig[i].PIMItemName;
                row[1] = lstConfig[i].BandAlias;
                row[2] = lstConfig[i].RCUIndex;
                row[3] = lstConfig[i].TestAngle;
                row[4] = lstConfig[i].SpecReg;
                dtShow.Rows.Add(row);
            }
            return lstConfig;
        }

        public override DataTable getProCodeTable()
        {
            var lstConfig = download();
            if (lstConfig == null)
            {
                return null;
            }
            return toTable(lstConfig);
        }
    }

}
